package com.run2code;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;


/**
 * Spring 简单使用示例
 * 关注公众号【奔跑的码畜】，一起进步不迷路
 *
 * @author 第七人格
 * @date 2024/01/19
 */
public class BeanFactoryDemo {

    private static final String FILE_NAME = "spring.xml";

    public static void main(String[] args) {
        // XmlBeanFactory
//        XmlBeanFactory demo = createXmlBeanFactory(FILE_NAME);
//        MyBean myBean = (MyBean) demo.getBean("myBean");
//        System.out.println(myBean.getName());


        // 测试ignoreDependencyInterface方法到底在干什么
        // 当实现了BeanNameAware接口时，输出的结果为：ignoredDependencyInterfacesTest
        // 否则，输出的结果为：myName
        // 也就是说，在Spring中有一些接口的属性，比如：BeanNameAware、BeanFactoryAware、BeanClassLoaderAware等，是不可以由外部随意注入的
        XmlBeanFactory demo = createXmlBeanFactory(FILE_NAME);
        IgnoredDependencyInterfacesTest myBean = (IgnoredDependencyInterfacesTest) demo.getBean("ignoredDependencyInterfacesTest");
        System.out.println(myBean.getBeanName());


        // 测试ClassPathXmlApplicationContext
//        ClassPathXmlApplicationContext demo = createApplicationContext(FILE_NAME);
//        MyBean myBean = (MyBean) demo.getBean("myBean");
//        System.out.println(myBean.getName());
    }

    public  static XmlBeanFactory createXmlBeanFactory(String fileName) throws BeansException {
        return new XmlBeanFactory(new ClassPathResource(fileName));
    }

    public  static ClassPathXmlApplicationContext createApplicationContext(String fileName) throws BeansException {
        return new ClassPathXmlApplicationContext(fileName);
    }


}
